package com.enums.cor;

import com.onjava.Enums;

import java.util.Iterator;

/**
 * @author ANG.YU
 * @date 2020-08-07 09:49
 */
public class Mail {
    static long counter = 0;
    GeneralDelivery generalDelivery;
    Scannability scannability;
    Readability readability;
    Address address;
    ReturnAddress returnAddress;
    long id = counter++;

    //生成测试的mail
    public static Mail randomMail() {
        Mail m = new Mail();
        m.generalDelivery = Enums.random(GeneralDelivery.class);
        m.scannability = Enums.random(Scannability.class);
        m.readability = Enums.random(Readability.class);
        m.address = Enums.random(Address.class);
        m.returnAddress = Enums.random(ReturnAddress.class);
        return m;
    }

    public static Iterable<Mail> generator(final int count) {
        return new Iterable<Mail>() {
            int n = count;

            @Override
            public Iterator<Mail> iterator() {
                return new Iterator<Mail>() {
                    @Override
                    public boolean hasNext() {
                        return n-- > 0;
                    }

                    @Override
                    public Mail next() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }

    @Override
    public String toString() {
        return "Mail " + id;
    }

    public String details() {
        return toString() +
                ", General Delivery: " + generalDelivery +
                ", Address Scanability: " + scannability +

                ", Address Readability: " + readability +

                ", Address Address: " + address +

                ", Return address: " + returnAddress;
    }

    enum GeneralDelivery {YES, NO1, NO2, NO3, NO4, NO5}

    enum Scannability {UNSCANNABLE, YES1, YES2, YES3, YES4}

    enum Readability {ILLEGIBLE, YES1, YES2, YES3, YES4}

    enum Address {INCORRECT, OK1, OK2, OK3, OK4, OK5, OK6}

    enum ReturnAddress {MISSING, OK1, OK2, OK3, OK4, OK5}

    public static class PostOffice {
        enum MailHandler {
            GENERAL_DELIVER {
                @Override
                boolean handle(Mail m) {
                    switch (m.generalDelivery) {
                        case YES:
                            System.out.println("Using general delivery for" );
                            return true;
                        default:
                            return false;
                    }
                }
            },
            MACHINE_SCAN {
                @Override
                boolean handle(Mail m) {
                    switch (m.scannability) {
                        case UNSCANNABLE:
                            return false;
                        default:
                            return false;
                    }
                }
            };

            abstract boolean handle(Mail m);
        }

    }


}
